/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 刘远望
 * Date: 2025-06-15
 * Time: 10:10
 */
//二分算法
public class Solution {
    public int[] searchRange(int[] nums, int target) {
        int[] ret =  {-1, -1};

        if (nums.length == 0) {
            return ret;
        }

        //二分左端点
        int left = 0;
        int right = nums.length - 1;

        while (left < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] < target) {
                left = mid + 1;
            }else {
                right = mid;
            }
        }

        //判断左端点的下标值是不是目标值
        if (nums[left] != target) {
            return ret;
        }else {
            //等于目标值更新左端点目标值
            ret[0] = left;
        }

        //二分右端点
        //找右端点left没有必要变
        right = nums.length - 1;

        while (left < right) {
            int mid = left + (right - left + 1) / 2;
            if (nums[mid] <= target) {
                left = mid;
            }else {
                right = mid - 1;
            }
        }
        //更新右端点结果 没必要再验证是不是等于目标值了 左端点已经验证过右目标值了
        ret[1] = right;

        /*//找右端点的值
        //找右端点值的另一个版本
        while (nums[right] == target) {
            right++;
            if (right >= nums.length) {
                break;
            }
        }

        ret[1] = right - 1;
        return ret;*/

        return ret;
    }
}
